package com.luozongyao.eduUcenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.luozongyao.commonutils.JwtUtils;
import com.luozongyao.commonutils.MD5;
import com.luozongyao.eduUcenter.entity.UcenterMember;
import com.luozongyao.eduUcenter.entity.vo.RegisterVo;
import com.luozongyao.eduUcenter.mapper.UcenterMemberMapper;
import com.luozongyao.eduUcenter.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.luozongyao.servicebase.config.exceptionHandler.lzyException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.UUID;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-08-20
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;


    //会员登录
    @Override
    public String login(UcenterMember member) {
        //获取手机号和密码
        //String mobile = member.getMobile();
        String password = member.getPassword();
        String email = member.getEmail();
//        if(StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)) {
//            throw new lzyException(20001,"登录失败");
//        }
        if(StringUtils.isEmpty(email) || StringUtils.isEmpty(password)) {
            throw new lzyException(20001,"登录失败");
        }
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
//        wrapper.eq("mobile",mobile);
        wrapper.eq("email",email);
        UcenterMember selectOne = baseMapper.selectOne(wrapper);
        if (selectOne == null){
            throw new lzyException(20001,"登录失败");
        }
        //判断密码
        //因为存储到数据库密码肯定加密的
        //把输入的密码进行加密，再和数据库密码进行比较
        // 加密方式MD5
        if(!MD5.encrypt(password).equals(selectOne.getPassword())){
            throw new lzyException(20001,"登录失败");
        }
        //校验是否被禁用
        if (selectOne.getIsDisabled()){
            throw new lzyException(20001,"登录失败");
        }
        //登录成功
        //生成token字符串，使用jwt工具类
        String jwtToken = JwtUtils.getJwtToken(selectOne.getId(), selectOne.getNickname());
        return jwtToken;
    }

    //注册方法
    @Override
    public void register(RegisterVo registerVo) {
        //获取注册信息，进行校验
        //昵称
        String nickname = registerVo.getNickname();
        String email = registerVo.getEmail();
        String password = registerVo.getPassword();
        String code = registerVo.getCode();
        //校验参数
        if(StringUtils.isEmpty(nickname) ||
        StringUtils.isEmpty(email) ||
        StringUtils.isEmpty(password) ||
        StringUtils.isEmpty(code)) {
            throw new lzyException(20001,"注册失败");
        }
        //校验校验验证码
        //从redis获取发送的验证码
        String emailCode = redisTemplate.opsForValue().get(email);
        if (!code.equals(emailCode)){
            throw new lzyException(20001,"验证码错误");
        }
        //查询数据库中是否存在相同的邮箱
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("email",email);
        Integer count = baseMapper.selectCount(wrapper);
        if (count>0){
            throw new lzyException(20001,"该邮箱已存在");
        }
        //添加注册信息到数据库
        UcenterMember ucenterMember = new UcenterMember();
        ucenterMember.setNickname(nickname);
        ucenterMember.setEmail(email);
        ucenterMember.setPassword(MD5.encrypt(password));
        ucenterMember.setIsDisabled(false);
        ucenterMember.setAvatar("https://edu-lzy0916.oss-cn-guangzhou.aliyuncs.com/2021/08/12/15baefea3ab143a3bff178bb41dccb51multipartFile.png");
        this.save(ucenterMember);
    }

    @Override
    public UcenterMember getUserByOpenId(String openid) {
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("openid",openid);
        UcenterMember member = baseMapper.selectOne(wrapper);
        return member;
    }

    @Override
    public Integer registerCount(String day) {
        return baseMapper.selectRegisterCount(day);
    }
}
